//
//  WBNewFeatureView.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/10/14.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit
// 新特性视图
class WBNewFeatureView: UIView, UIScrollViewDelegate{

    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var enterButton: UIButton!
    
    @IBOutlet weak var pagerControl: UIPageControl!
    
    class func newFeatureView() -> WBNewFeatureView {
        
        let nib = UINib.init(nibName: "WBNewFeatureView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: [:])[0] as! WBNewFeatureView
        
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    
    override func awakeFromNib() {
        print(bounds)
        
        let count = 4
        
        let rect = UIScreen.main.bounds
        
        for i in 0..<count {
            
            let imageName = "new_feature_\(i+1)"
            
            guard let image = UIImage(named: imageName) else {
                return
            }
            
            let imageView = UIImageView(image: image)
            
            imageView.frame = rect.offsetBy(dx: rect.width * CGFloat(i),  dy: 0)
            
            scrollView.addSubview(imageView)
        }
        scrollView.contentSize = CGSize(width: rect.width * CGFloat(count + 1), height: rect.height)
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        
        enterButton.isHidden = true
        scrollView.delegate = self
        
        
    }
    //进入微博
    @IBAction func enterWeibo(_ sender: AnyObject) {
        removeFromSuperview()

    }
    
    
}


extension WBNewFeatureView {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        print(page)
        
        if page == scrollView.subviews.count {
            
            removeFromSuperview()
        }
        
        enterButton.isHidden = (page != scrollView.subviews.count-1)
        
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        enterButton.isHidden = true
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        pagerControl.currentPage = page
        
        pagerControl.isHidden = (page == scrollView.subviews.count)
        
    }
    
}
